﻿package
{
	import flash.display.Sprite;
	import flash.events.*;
	import gs.TweenLite;
	import fl.motion.easing.*;
	
	public class Fisheye extends Sprite
	{	
		//Global variables
		private var arr:Array = new Array(5);
		private var yPos:Number = stage.stageHeight / 2;
		private var xPos:Number; 
		private var fullWidth:Number;
		private var posMouse:Number;
		private var flag:Boolean = true;
		private var radius:Number = 125;
		
		//Constructor
		public function Fisheye()
			{
				arr[0] = new mcHealth();
				arr[1] = new mcKids();
				arr[2] = new mcNews();
				arr[3] = new mcOrganics();
				arr[4] = new mcRecipes();	
				addItems();
				setXpos();
				stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
				stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);
			}
			
		//Functions	
		private function addItems():void
		{
			
			for (var i:uint = 0; i < 5; i++)
			{
				arr[i].scaleX = arr[i].scaleY = 0.5;
				arr[i].y = yPos;
				addChild(arr[i]);
			}
		}
		
		private function setXpos():void
		{
			fullWidth = 0;
		
			for (var i:uint = 0; i < 5; i++)
			{
				fullWidth += arr[i].width;
			}
			
			xPos = ((stage.stageWidth - fullWidth) / 2) + (arr[0].width / 2)
			
			for (var j:uint = 0; j < 4; j++)
			{
				arr[j].x = xPos;
				xPos += (arr[j].width / 2) + (arr[j+1].width / 2);
			}
			
			arr[4].x = xPos;
		}
		
		//Events
		private function onMouseMove($evt:Event):void
		{	
	
			setXpos();
			
			for (var r:uint=0; r < 5; r++)
			{
				if (Math.abs(mouseX - arr[r].x) < radius)
				{
					arr[r].scaleX = arr[r].scaleY = 1- (Math.abs(mouseX - arr[r].x) * (0.5 / radius));
				}
				else
				{
					arr[r].scaleX = arr[r].scaleY = 0.5;
				}
			}
		}
			
		private function mouseLeave($evt:Event):void
		{
			
			for (var r:uint=0; r < 5; r++)
			{
					arr[r].scaleX = arr[r].scaleY = 0.5;
			}
			
			setXpos();
		}
	}
}